﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Modellayer;
using System.Threading;
using System.Globalization;
using System.Data;

namespace Dblayer
{
    public class DbShow
    {
        SqlCommand cmd = null;
        DbShowRoom dbSh;
        DbMovie dbM;

        public DbShow()
        {
            dbSh = new DbShowRoom();
            dbM = new DbMovie();
        }

        public void createShow(Show s)
        {
            try
            {
                string insert = @"insert into Show(ShowId, ShowTime, ShowRoomNumber, Title) 
                    values ('" + s.showId + "'," + "'" + s.showTime + "'," + "'" + s.showroom.showRoomNumber + "'," + "'" +
                                       s.movie.Title + "')";
                cmd = Connection.GetDBCommand(insert);
                cmd.ExecuteReader();
            }
            catch (Exception e)
            {

                Console.WriteLine("{0}", e.Message);
            }
            finally
            {
                if (Connection.dbconn != null)
                {
                    Connection.dbconn.Close();
                }
            }
        }

        public Show findShow(int ShowId)
        {
            string sql = "SELECT * FROM Show WHERE ShowId = '" + ShowId + "'";
            return buildShow(sql);
        }

        public List<Show> findAllShows()
        {
            string sql = "SELECT * FROM Show";
            return buildAllShows(sql);
        }




        private Show buildShow(string show)
        {
            IDataReader dbReader;
            Show s = null;
            Show showss = null;
            int ss = 0;
            string getShow = "";


            try
            {

                cmd = Connection.GetDBCommand(show);
                dbReader = cmd.ExecuteReader();
                while (dbReader.Read())
                {
                    s = new Show();
                    s.showId = int.Parse(dbReader["ShowId"].ToString());
                    s.showTime = dbReader["ShowTime"].ToString();
                    getShow = dbReader["Title"].ToString();
                    ss = int.Parse(dbReader["showRoomNumber"].ToString());
                    int showIdforSeats = s.showId;
                    //s.createSeatsOnNewShow(showIdforSeats);

                }
                Connection.dbconn.Close();
                Movie m = dbM.findMovie(getShow);
                ShowRoom sh = dbSh.findShowRoom(ss);
                s.movie = m;
                s.showroom = sh;
                showss = new Show(m, sh, s.showId, s.showTime);
                

            }
            catch (Exception e)
            {
                Console.WriteLine("Could not find the show on specified ID! " + e.Message);
                s = null;
            }
            return showss;
        }


        private List<Show> buildAllShows(string show)
        {

            List<Show> sList = new List<Show>();
            IDataReader dbReader;
            Show s = null;
            int ss = 0;
            string getShow = "";
            bool found = false;
            bool found2 = false;


            try
            {

                cmd = Connection.GetDBCommand(show);
                dbReader = cmd.ExecuteReader();
                while (dbReader.Read())
                {
                    
                        s = new Show();
                        s.showId = int.Parse(dbReader["ShowId"].ToString());
                        s.showTime = dbReader["ShowTime"].ToString();
                        ss = int.Parse(dbReader["showRoomNumber"].ToString());
                        getShow = dbReader["Title"].ToString();
                    
                    if (s.showroom == null || found == false)
                    {
                        s.showroom = new ShowRoom();
                     
                        s.showroom.showRoomNumber = ss;
                        found = true;
                    }
                    if (s.movie == null || found2 == false)
                    {
                        s.movie = new Movie();
                       
                        s.movie.Title = getShow;
                        found2 = true;
                    }
                        sList.Add(s);
                        found = false;
                        found2 = false;




                }
                Connection.dbconn.Close();
                dbReader.Close();

            }
            catch (Exception e)
            {
                Console.WriteLine("Could not find the show - ** READ EXECPTION!\n\n " + e.Message);
                s = null;
             
                sList = null;
            }
            return sList;
        }






        public void updateShow(Show s)
        {
            try
            {
                string update = @"UPDATE Show SET " + "ShowTime = '" + s.showTime
                    + "', " + "ShowRoomNumber = '" + s.showroom.showRoomNumber + "', " + "Title = '" + s.movie.Title + "' " + "WHERE ShowId = " + s.showId;
                cmd = Connection.GetDBCommand(update);
                cmd.ExecuteReader();
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}", e.Message);
            }
            finally
            {
                if (Connection.dbconn != null)
                {
                    Connection.dbconn.Close();
                }
            }
        }

    }
}
